//
//  LikeViewController.m
//  网络FM
//
//  Created by dengzhicheng on 15/5/26.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//

#import "LikeViewController.h"
#import "CellectDatabase.h"
#import "CellectModel.h"
#import "LikeTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "PlayerViewControllerCellectAndHistory.h"

@interface LikeViewController ()<UITableViewDataSource, UITableViewDelegate>


@end

@implementation LikeViewController

- (void)dealloc
{
    [_likeTableView release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    [self tableView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

- (void)createView
{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    [navView setBackgroundColor:[UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0f]];
    [self.view addSubview:navView];
    [navView release];
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(25, 25, 30, 30)];
    [backButton setBackgroundColor:[UIColor clearColor]];
    [backButton setImage:[UIImage imageNamed:@"返回.jpg"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"返回.jpg"] forState:UIControlStateHighlighted];
    [backButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:backButton];
    [backButton release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 80) / 2, 25, 80, 30)];
    titleLabel.text = @"我的收藏";
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [navView addSubview:titleLabel];
    [titleLabel release];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 1)];
    [view setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:view];
    [view release];
}
- (void)tableView
{
    
    self.likeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    _likeTableView.dataSource = self;
    _likeTableView.delegate = self;
    _likeTableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:_likeTableView];
    [_likeTableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"likeCell";
    LikeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[LikeTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
    }
    CellectModel *model = [self.array objectAtIndex:indexPath.row];
    
    [cell.likeTitle setText:model.titleName];
    [cell.likePeople setText:model.people];
    [cell.likeImageView sd_setImageWithURL:[NSURL URLWithString:model.imageName] placeholderImage:[UIImage imageNamed:@"占位图.jpg"] options:SDWebImageRetryFailed];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.likeTableView deselectRowAtIndexPath:[self.likeTableView indexPathForSelectedRow] animated:YES];
    PlayerViewControllerCellectAndHistory *player = [[PlayerViewControllerCellectAndHistory alloc] init];
    CellectModel *model = [self.array objectAtIndex:indexPath.row];
    player.urlForPlay = model.videoUrl;
    player.picURL = model.imageName;
    player.tit = model.titleName;
    
    UIView *controlView = [self.view.window.subviews objectAtIndex:1];
    [controlView setAlpha:0];
    
    [self presentViewController:player animated:YES completion:^{
        
    }];
    [player release];
}


#pragma mark -
#pragma mark 第三步:设置cell的编辑样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma mark -
#pragma mark 第四步:提交编辑结果

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellectModel *model = [_array objectAtIndex:indexPath.row];
    [CellectDatabase deleteWithBookId:model];
    [self.array removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
}

- (void)backButtonClick:(UIButton *)button
{
    
    // 抽屉状态恢复
    [[NSNotificationCenter defaultCenter] postNotificationName:@"huifu" object:nil];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


@end
